package educar.controllers;


import javax.swing.JOptionPane;
import javax.swing.JTable;

import educar.gui.IView;
import educar.idioma.Locale;
import educar.models.Materia;
import educar.models.materiaNotFound;



/**
 * Permite borrar una materia
 */
public class DeleteMateriaController implements IController {
	private IView panel;
	private JTable table;
	
	/**
	 * Constructor
	 * @param table "JTable sobre la cual se busca la materia a borrar"
	 */
	public DeleteMateriaController(JTable table) {
		this.table = table;
	}


	public void process(String model){
		/*obtengo la fila seleccionada*/
		final int fila = table.getSelectedRow();
		if (fila == -1) {
			JOptionPane.showMessageDialog(null,Locale.FIRST_SELECT_ROW,Locale.NOT_ROW_SELECTED,JOptionPane.WARNING_MESSAGE);
		}
		else {
			Materia old;
			try {
				old = Materia.getMateriaByCodigo((table.getModel().getValueAt(fila, 1).toString()));
				int n = JOptionPane.showConfirmDialog(null,Locale.DELETE+" "+Locale.ASSIGNAMENT+": "+old.getCodigo()+" "+old.getNombre(),Locale.DELETE+" "+Locale.ASSIGNAMENT,JOptionPane.YES_NO_OPTION);
				if (n == JOptionPane.OK_OPTION) {
							old.destroy();
							/*uso el present para refrescar la tabla*/
							panel.present(model);			
						}		
			} catch (materiaNotFound e) {
				JOptionPane.showMessageDialog(null,Locale.FACULTY+" "+Locale.NOT_FOUND,Locale.NOT_FOUND,JOptionPane.WARNING_MESSAGE);
			}
		}
		
	}

	public void setView(IView view) {
		this.panel = view;
	}

}
